﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Input;
using ZFCNew.Code;
using ZFCNew.ZfcServiceReference;
using System.Windows;

namespace ZFCNew.ViewModels
{
    public class PhotoGalleryModel : INotifyPropertyChanged
    {
        private ImageInfo _currentImage;
        public ICommand GoNextCommand { get; set; }
        public ICommand GoPrevCommand { get; set; }
        
       
        public int CurrentIndex { get; private set; }
       
        public bool CanGoNext
        {
            get { return ImageColl != null && CurrentIndex < ImageColl.Count - 1; }
        }
        public bool CanGoPrev
        {
            get { return ImageColl != null && CurrentIndex > 0; }
        }

        public ImageInfo PrevImage { get; private set; }
       
        public ImageInfo NextImage { get; private set; }


        public ObservableCollection<ImageInfo> ImageColl { get; private set; }

        public ImageInfo CurrentImage
        {
            get { return _currentImage; }
            set 
            {
                _currentImage = value;
                NotifyPropertyChanged("CurrentImage");
                NotifyPropertyChanged("CanGoNext");
                NotifyPropertyChanged("CanGoPrev");
            }
        }


        public PhotoGalleryModel()
        {
            GoNextCommand = new DelegateCommand(GoNext, (s) => CanGoNext);
            GoPrevCommand = new DelegateCommand(GoPrev, (s) => CanGoPrev);
            LoadImages();
        }

        public  void DisplayImage(int  id)
        {
            if(id>=0 && id <= ImageColl.Count-1)
            {
                CurrentIndex = id;
                CurrentImage = ImageColl[id];
            }
        }

        private void LoadImages()
        {
           ServiceHelper hp = new ServiceHelper();
           hp.GetImageCompleted += new EventHandler<DataEventArgs<ObservableCollection<ImageInfo>>>(hp_GetImageCompleted);
           hp.GetImage();
        }

        void hp_GetImageCompleted(object sender, DataEventArgs<ObservableCollection<ImageInfo>> e)
        {
           if(e.IsSuccess)
           {
               ImageColl = e.Data;
               if (ImageColl.Count > 0) CurrentImage = ImageColl[0];
               Deployment.Current.Dispatcher.BeginInvoke(() => { NotifyPropertyChanged("ImageColl"); });
           }
        }

        public void GoNext(object sender)
        {
            if(CanGoNext)
            {
                CurrentIndex++;
                CurrentImage = ImageColl[CurrentIndex];
            }
        }

        public void GoPrev(object sender)
        {
            if (CanGoPrev)
            {
                CurrentIndex--;
                CurrentImage = ImageColl[CurrentIndex];
            }
        }

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
    }
}